package model;


// Generated Jul 16, 2011 12:25:21 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.stat.Statistics;

import utils.HibernateUtil;

/**
 * Home object for domain model class Account.
 * @see Model.Account
 * @author Hibernate Tools
 */
public class AccountHome implements AccountDAO{

    private static final Log log = LogFactory.getLog(AccountHome.class);

//    private final SessionFactory sessionFactory = getSessionFactory();

    private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    private Session session = HibernateUtil.getSession();
    
    Statistics stats = sessionFactory.getStatistics();
    
    
    protected SessionFactory getSessionFactory() {
        try {
            return (SessionFactory) new InitialContext()
                    .lookup("SessionFactory");
        } catch (Exception e) {
            log.error("Could not locate SessionFactory in JNDI", e);
            throw new IllegalStateException(
                    "Could not locate SessionFactory in JNDI");
        }
    }
    
    protected void init(){

    }

    public void persist(Account transientInstance) {
        stats.setStatisticsEnabled(true);
        Transaction tx = session.beginTransaction();
        session.save(transientInstance);
        tx.commit();
        
        stats.getSessionOpenCount();
        stats.logSummary();
        session.close();
        
        log.debug("persisting Account instance");
        try {
//            sessionFactory.getCurrentSession().persist(transientInstance);
            
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void attachDirty(Account instance) {
        log.debug("attaching dirty Account instance");
        try {
            sessionFactory.getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    @SuppressWarnings("deprecation")
    public void attachClean(Account instance) {
        log.debug("attaching clean Account instance");
        try {
            sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void delete(Account persistentInstance) {
        log.debug("deleting Account instance");
        try {
            sessionFactory.getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Account merge(Account detachedInstance) {
        log.debug("merging Account instance");
        try {
            Account result = (Account) sessionFactory.getCurrentSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public Account findById(int id) {
        log.debug("getting Account instance with id: " + id);
        try {
            Account instance = (Account) sessionFactory.getCurrentSession()
                    .get("Model.Account", id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(Account instance) {
        log.debug("finding Account instance by example");
        try {
            List results = sessionFactory.getCurrentSession()
                    .createCriteria("Model.Account")
                    .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }
}

